var moveNumber = 0;
function supportsHtml5Storage() {
	try {
		return 'localStorage' in window && window['localStorage'] !== null;
	} catch (e) {
		return false;
	}
}

function getPreviousGameCount() {
	return localStorage["gameIndex"];
}

function initializeCurrentGameIndex(){
	var gameIndex = localStorage["gameIndex"];
	if (gameIndex == null){
		gameIndex=1;
		localStorage["gameIndex"]=gameIndex;
	}else{
		gameIndex++;
		localStorage["gameIndex"]=gameIndex;
	}
}

function getCurrentGameIndexFromLocalStorage(){
	return localStorage["gameIndex"];
}

function createListFromLocalStorage() {
	var previousGameCount = getPreviousGameCount();

	for (var i = 1; i <= previousGameCount; i++) {
		var playerOne = localStorage["g"+i+"player1"];
		var playerTwo = localStorage["g"+i+"player2"];
		var gameRecord = "<b>"+playerOne + "</b> --- VS --- <b>" + playerTwo+"</b>";
		var wrapper = i+". " + gameRecord+ "<button id='" + "game"
				+ i + "' class='watchgame'>Watch this game</button></br>";

		$("#previousgameslist").append(wrapper);

	}
	if(previousGameCount == null){
		var message = "<h3> You have not played before! </h3>";
		$("#previousgameslist").html(message);
	}
}

function saveMove(data) {
	if (!supportsHtml5Storage()) {
		return alert("Sorry your browses does not support HTML5 storage.");
	}
	

	localStorage["move" + moveNumber] = data;
	console.log("Data:" + localStorage["move" + moveNumber]
			+ " has been added to the local storage!");
	moveNumber++;

}

function saveGameState() {
	if (!supportsLocalStorage()) {
		return false;
	}
	localStorage["halma.game.in.progress"] = gGameInProgress;
	for ( var i = 0; i < kNumPieces; i++) {
		localStorage["halma.piece." + i + ".row"] = gPieces[i].row;
		localStorage["halma.piece." + i + ".column"] = gPieces[i].column;
	}
	localStorage["halma.selectedpiece"] = gSelectedPieceIndex;
	localStorage["halma.selectedpiecehasmoved"] = gSelectedPieceHasMoved;
	localStorage["halma.movecount"] = gMoveCount;
	return true;
}
